package com.tanhua.web;

import com.tanhua.domain.Admin;
import com.tanhua.domain.AdminHolder;
import com.tanhua.domain.AdminVo;
import com.tanhua.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/system")
public class AdminController {

    @Autowired
    private AdminService adminService;


    @GetMapping(value = "/users/verification",name="获取验证码图片")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws IOException {
        return adminService.verification(uuid,response);
    }

    @PostMapping(value = "/users/login",name="用户登录")
    public ResponseEntity login(@RequestBody Map<String,String> map) throws IOException {
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        return adminService.login(username,password,verificationCode,uuid);
    }



    @PostMapping(value = "/users/profile",name="获取用户的基本信息")
    public ResponseEntity profile() throws IOException {
        //根据传递的token 去redis获取登录用户的信息
        Admin admin = AdminHolder.getAdmin();
        AdminVo vo = new AdminVo();
        BeanUtils.copyProperties(admin,vo);
        //返回用户的基本信息
        return ResponseEntity.ok(vo);
    }



    @PostMapping(value = "/users/logout",name="用户退出")
    public ResponseEntity logout() throws IOException {
        //根据token将当前用户做删除
        return adminService.logout();
    }
}
